package com.jjt.kiki.crawler;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import org.wltea.analyzer.lucene.IKAnalyzer;

import com.jjt.kiki.bean.Res;

public class LuceneUtil {
	private static final String indexDir = "../index-dir";

	private LuceneUtil() {
	};

	private static class Single {
		private static final LuceneUtil instance = new LuceneUtil();
	}

	public static LuceneUtil getInstance() {
		return Single.instance;
	}

	public void index(List<Res> reses) throws IOException {
		List<Document> docs = new ArrayList<Document>();
		for (Res res : reses) {
			Document doc = new Document();
			// 定义主题
			TextField title = new TextField("title", res.getResTitle(), Field.Store.NO);
			// 定义id
			StringField id = new StringField("id", res.getResId(), Field.Store.YES);
			doc.add(id);
			doc.add(title);
			docs.add(doc);
		}
		IndexWriter writer = null;
		Directory dir = null;
		dir = FSDirectory.open(this.getIndexDir().toPath());
		IKAnalyzer analyzer = new IKAnalyzer();
		IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
		writer = new IndexWriter(dir, iwc);
		writer.addDocuments(docs);
		writer.close();
	}

	public File getIndexDir() {
		return new File(indexDir);
	}

	public static void main(String[] args) throws IOException, ParseException {
//		indexTest();
		Directory dir = FSDirectory.open(LuceneUtil.getInstance().getIndexDir().toPath());
		DirectoryReader ireader = DirectoryReader.open(dir);
		IndexSearcher isearcher = new IndexSearcher(ireader);
		// Parse a simple query that searches for "text":
		QueryParser parser = new QueryParser("title", new IKAnalyzer());
		Query query = parser.parse("torrent");
		ScoreDoc[] hits = isearcher.search(query, 10, Sort.INDEXORDER).scoreDocs;
		// Iterate through the results:
		for (int i = 0; i < hits.length; i++) {
			Document hitDoc = isearcher.doc(hits[i].doc);
			System.out.println(hitDoc.get("id"));
		}
		ireader.close();
		dir.close();
	}

	private static void indexTest() throws IOException {
		Res res1 = new Res();
		res1.setResId("1");
		res1.setResTitle("【国漫】国M老公带回家 第二季 第02话.Mp4.torrent等");
		Res res2 = new Res();
		res2.setResId("2");
		res2.setResTitle("没钱开会员云盘空间不够怎么办？");
		Res res3 = new Res();
		res3.setResId("3");
		res3.setResTitle("老人交响乐团.オケ老人.2016.BD720P.高清日语中字.torrent");
		Res res4 = new Res();
		res4.setResId("4");
		res4.setResTitle("我们.Atashira.2017.720p.日语简繁中字.5.46GB.torrent");
		Res res5 = new Res();
		res5.setResId("5");
		res5.setResTitle("疯岳撬佳人.2017.1080P.WEB-DL.X264.AAC.CHS-BT4K.torrent等");
		Res res6 = new Res();
		res6.setResId("6");
		res6.setResTitle("新娘.2017.HD720P.俄语中字.torrent");
		Res res7 = new Res();
		res7.setResId("7");
		res7.setResTitle("【限制级】禁止的爱：情深深等");
		Res res8 = new Res();
		res8.setResId("8");
		res8.setResTitle("少年 你对群马一无所知");
		Res res9 = new Res();
		res9.setResId("9");
		res9.setResTitle("乡村医生.2016.HD720P.法语中字.torrent");
		Res res10 = new Res();
		res10.setResId("10");
		res10.setResTitle("攻壳机动队.2017.HD720P.韩版中字.torrent");

		List<Res> list = new ArrayList<Res>();
		list.add(res10);
		list.add(res9);
		list.add(res8);
		list.add(res7);
		list.add(res6);
		list.add(res5);
		list.add(res4);
		list.add(res3);
		list.add(res2);
		list.add(res1);
		LuceneUtil.getInstance().index(list);
	}
}
